package cn.qg.trade.controller;

import cn.qg.beans.dto.Dto;
import cn.qg.beans.pojo.QgGoods;
import cn.qg.beans.pojo.QgGoodsTempStock;
import cn.qg.beans.pojo.QgUser;
import cn.qg.common.DtoUtil;
import cn.qg.trade.service.goods.GoodsService;
import cn.qg.trade.service.qgGoodsTempStock.QgGoodsTempStockService;
import cn.qg.trade.service.token.TokenServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 15:52
 */
@RestController
public class OrderController {
    @Resource
    GoodsService goodsService;
    @Resource
    QgGoodsTempStockService qgGoodsTempStockService;
    @Resource
    TokenServiceImpl tokenServiceImpl;
    @RequestMapping(value = "/api/flushIsGet")
    public Dto flushIsGet(@RequestParam String  goodsId,@RequestParam String  token){
        try {
            //判断token 是否过期
            QgUser qgUser = tokenServiceImpl.getItripUserByToken(token);
            if(qgUser==null){
                return DtoUtil.returnFail("token已经过期","0001");
            }
            //判断是否有库存
            QgGoods qgGoods= goodsService.getGoodsById(goodsId);
            if(qgGoods.getStock()<=0){
                return DtoUtil.returnFail("抢购失败","0002");
            }


            //判断是否已经抢购状态0：未支付；1：已经抢购过
            QgGoodsTempStock qgGoodsTempStock=qgGoodsTempStockService.getGoodsTempStockBy(goodsId,qgUser.getId());
            if (qgGoodsTempStock.getStatus()==1){
                return DtoUtil.returnDataSuccess("-1");
            }else if (qgGoodsTempStock.getStatus()==0){
                return DtoUtil.returnDataSuccess("2");
            }
            return DtoUtil.returnDataSuccess("3");

        }catch (Exception e){
            e.getMessage();
            return DtoUtil.returnFail(e.getMessage(),"0000");
        }


    }
}
